<template>
  <div style="height:100%;width:100%;position:relative;">
      <div class="pagetop">
          <template>
              <div style="width:100%;">
                  <div style="float:left;width:50%">
                      <table style="font-size:12px" class="table1style">
                          <tr>
                              <td>快速查询</td>
                              <td>
                                  <el-input v-model.trim="fuzzyquery" placeholder="医生代码|医生名称|输入码" style="width:180px" clearable></el-input>
                              </td>
                              <!-- <td v-if="power4Type">医院</td>
                              <td>
                                  <el-select v-if="power4Type" v-model="ddlhospcode" placeholder="请选择" @change="btnQuery" style="width:230px" clearable>
                                      <el-option :key="item.value" :value="item.value" :label="item.label" v-for="item in HospitalOptions"></el-option>
                                  </el-select>
                              </td> -->
                              <td v-if="power4Type">医院</td>
                              <td v-if="power4Type">
                                  <rm-ddl-common @change="btnQuery" datatype="hospital" dataname="医院" v-model="ddlhospcode" width="180px"></rm-ddl-common>
                              </td>
                          </tr>
                      </table>
                  </div>
                  <div style="float:left;width:50%;">
                      <table style="float:right">
                          <tr>
                              <td style="align:right">
                                  <el-button v-has="'PINQUERY'" type="primary" icon="el-icon-search" @click="btnFresh">查询</el-button>
                              </td>
                              <td style="align:right">
                                  <el-button v-has="'PADD'" icon="el-icon-edit-outline" @click="btnAdd">新增</el-button>
                              </td>
                              <td style="align:right">
                                  <el-button v-has="'PDELETE'" icon="el-icon-delete" @click="btnDelete">删除</el-button>
                              </td>
                              <td style="align:right">
                                  <el-button icon="el-icon-document" @click="btnSave">保存</el-button>
                              </td>
                              <td style="align:right">
                                  <el-button v-has="'PEXPORT'" @click="btnExport">导出</el-button>
                              </td>
                              <td style="align:right">
                                  <el-button v-has="'PSYNC'" icon="el-icon-position" @click="syncdata">同步</el-button>
                              </td>
                          </tr>
                      </table>
                  </div>
              </div>
          </template>
      </div>
      <div class="pagecenter">
          <div class="middle-center">
              <vxe-table show-overflow ref="xTable1" class="mytable-style" height="auto" :loading="bLoading" :select-config="{reserve: true}" :sort-config="{trigger: 'cell'}" :keyboard-config="keyConfig" :keyboard-local-config="keyLocalConfig" :mouse-config="{selected: true}" :data.sync="FuzzyQuery" border size="mini" show-header-overflow highlight-current-row highlight-hover-row resizable stripe :edit-config="{trigger: 'click',mode: 'cell',showStatus: true,showIcon: false,activeMethod:CellActiveMethod}" @edit-closed="EditClose" header-align="center" :row-class-name="RowClassName" :cell-class-name="CellClassName" @current-change="currentChangeEvent" :edit-rules="doctor_validRules">
                  <vxe-table-column type="index" title="NO." width="50"></vxe-table-column>
                  <vxe-table-column field="doctorno" title="医生代码" width="80" header-align="center" :edit-render="{name: 'input', autoselect: true}">
                      <template v-slot:header="{ column }">
                          <span style="color: #F56C6C;">医生代码</span>
                      </template>
                  </vxe-table-column>
                  <vxe-table-column field="doctorname" title="医生姓名" width="70" header-align="center" :edit-render="{name: 'input', autoselect: true}">
                      <template v-slot:header="{ column }">
                          <span style="color: #F56C6C;">医生姓名</span>
                      </template>
                  </vxe-table-column>
                  <vxe-table-column field="doctorenname" title="英文名称" width="70" header-align="center" :edit-render="{name: 'input', autoselect: true}">
                  </vxe-table-column>
                  <vxe-table-column field="his_doctorno" title="医生代码（HIS）" width="90" header-align="center" :edit-render="{name: 'input', autoselect: true}">
                      <template v-slot:header="{ column }">
                          <span style="color: #F56C6C;">医生代码（HIS）</span>
                      </template>
                  </vxe-table-column>
                  <vxe-table-column field="doctorkind" title="医生类别" width="90" align="center" :edit-render="{name: 'input', autoselect: true}">
                  </vxe-table-column>
                  <vxe-table-column field="shortmsgcode" title="短信通知号码/手机号码" width="150" :edit-render="{name: 'input', autoselect: true}">
                  </vxe-table-column>
                  <vxe-table-column field="inputcode" title="输入码" width="130" :edit-render="{name: 'input', autoselect: true}">
                  </vxe-table-column>
                  <vxe-table-column field="hospcode" title="所属医院" width="200" :edit-render="{name:'select', options: HospitalOptions}">
                  </vxe-table-column>
                  <vxe-table-column field="invalidflag" title="停用" width="50" align="center">
                      <template v-slot="{row}">
                          <el-checkbox v-if="row.invalidflag == '1'" checked="checked" :key="1" @change="InvalidFlag(row, 'invalidflag')"></el-checkbox>
                          <el-checkbox v-else :key="0" @change="InvalidFlag(row, 'invalidflag')"></el-checkbox>
                      </template>
                  </vxe-table-column>
                  <vxe-table-column field="idcardno" title="身份证号" width="200" :edit-render="{name: 'input', autoselect: true}">
                  </vxe-table-column>
              </vxe-table>
          </div>
      </div>
      <el-dialog v-el-drag-dialog :append-to-body="true" :close-on-click-modal="false" title="删除" :visible.sync="bDialogDelVisible" width="700px" height="200px">
          <span class="el-icon-info">确定删除医生名称为<b style="color:red">{{message}}</b>的记录吗？</span>
          <span slot="footer" class="dialog-footer">
              <el-button type="primary" @click="ConfirmDel">确 定</el-button>
              <el-button @click="bDialogDelVisible = false" :autofocus="true">取 消</el-button>
          </span>
      </el-dialog>
  </div>
</template>

<script type="text/javascript">
export default {
  name: "MNG212",
  data() {
      return {
          HospitalOptions: [],
          fuzzyquery: "",
          power4Type: false,
          bLoading: false,
          bAfterfirstloading: true,
          bDialogDelVisible: false,
          bQueryAll: true,
          tempindex: 0,
          tempData: [],
          message: "",
          Selectrow: [],
          tableData1: [],
          actionindex: "Q",
          ddlhospcode: "",
          doctorno: "", //医生代码
          doctorname: "", //医生姓名
          doctorkind: "", //医生类别
          hospcode: "", //病区代码(HIS)
          hospitalcode: "",
          shortmsgcode: "", //短信通知号码/手机号码
          inputcode: "", //输入码
          wardno: "", //所属院区
          invalidflag: 0, //停用
          idcardno: "", //身份证号
          keyConfig: {
              isDel: true,
              isTab: true,
              isEdit: true,
          },
          keyLocalConfig: {
              isArrow: true,
          },
          doctor_validRules: {
              doctorno: [{ required: true, message: "医生代码必须填写" }],
              doctorname: [{ required: true, message: "医生姓名必须填写" }],
              his_doctorno: [{ required: true, message: "医生代码(HIS)必须填写" }],
          },
      };
  },
  watch: {
      txtsearch(val, oldval) {
          if (val != oldval) {
              this.$nextTick(() => {
                  var row = this.$refs.xTable1.getCurrentRow();
                  if (this.$refs.xTable1.tableData.length > 0) row = this.$refs.xTable1.tableData[0];
                  if (row.doctorno) {
                      this.$refs.xTable1.setCurrentRow(row);
                      this.tempindex = this.$refs.xTable1.getRowIndex(row);
                  }
              });
          }
      },
  },
  computed: {
      FuzzyQuery() {
          let querystr = this.fuzzyquery.trim();
          if (querystr) {
              let rest = this.tableData1.filter((item) => item.doctorno.indexOf(querystr) > -1 || item.doctorname.indexOf(querystr) > -1 || (item.inputcode ? item.inputcode.toUpperCase().indexOf(querystr.toUpperCase()) > -1 : false));
              return rest;
          } else {
              return this.tableData1;
          }
      },
  },
  created() {
      this.btnQuery();
      this.ddlInitdata();
      this.$ddldata.IsAreaModeFun().then((r) => {
          this.GetUtype();
      }); //防止刷新页面导致IsAreaMode被清掉
  },
  mounted() {},
  updated() {
      if (this.bAfterfirstloading) {
          this.ComCallFun();
      }
  },
  methods: {
      ComCallFun() {
          let currentrow = "";
          if (this.actionindex != "D") {
              if (this.actionindex == "A") {
                  if (this.selectrow != "") {
                      this.$nextTick(() => {
                          var alld = this.$refs.xTable1.getTableData().fullData;
                          for (var i = 0; i < alld.length; i++) {
                              if (alld[i].doctorno == this.selectrow.doctorno) {
                                  this.tempindex = i;
                                  break;
                              }
                          }
                          currentrow = this.$refs.xTable1.getTableData().fullData[this.tempindex];
                      });
                  } else {
                      currentrow = this.$refs.xTable1.getTableData().fullData[0];
                  }
              } else if (this.actionindex == "M") {
                  this.$nextTick(() => {
                      var alld = this.$refs.xTable1.getTableData().fullData;
                      for (var i = 0; i < alld.length; i++) {
                          if (alld[i].doctorno == this.selectrow.doctorno) {
                              this.tempindex = i;
                              break;
                          }
                      }
                      currentrow = this.$refs.xTable1.getTableData().fullData[this.tempindex];
                  });
              } else {
                  currentrow = this.$refs.xTable1.getTableData().fullData[0];
              }
          } else {
              if (this.tempindex > 0) {
                  currentrow = this.$refs.xTable1.getTableData().fullData[this.tempindex];
              } else {
                  currentrow = this.$refs.xTable1.getTableData().fullData[0];
              }
          }
          //let currentrow = this.$refs.xTable1.getData(this.tempindex);
          this.$nextTick(() => {
              this.currRow = currentrow;
              if (currentrow) {
                  this.okFirstLoading = false;
                  this.$refs.xTable1.setCurrentRow(currentrow);
                  this.$refs.xTable1.scrollToRow(currentrow);
                  this.message = currentrow.doctorname;
                  //this.selectrow = currentrow;
                  this.wardno = currentrow.wardno;
                  this.hospitalcode = currentrow.hospcode;
              }
          });
      },
      btnQuery() {
          this.actionindex = "Q";
          this.bLoading = true;
          this.$tool
              .request({
                  url: "MNG/MNG2/MNG212",
                  method: "post",
                  data: {
                      action: "query",
                      ddlhospcode: this.ddlhospcode,
                  },
              })
              .then((response) => {
                  this.tableData1 = response.Data;
                  // this.tableData1.sort((a, b) => {
                  //     return a.invalidflag - b.invalidflag;
                  // });
                  this.bLoading = false;
                  this.bAfterfirstloading = true;
              })
              .catch((e) => {
                  console.log("error", e);
                  this.bLoading = false;
              });
      },
      InvalidFlag(row, col) {
          let iInvalidCell = row[col];
          row[col] = iInvalidCell == "1" ? 0 : 1;
      },
      EditClose({ row, column, rowIndex }) {
          //写法错误，且代码无意义
          // if (column.property == "inputcode") {
          //     this.$refs.xTable1.$refs.tableBody.$children[rowIndex].$el.focus();
          // } else
          if (column.property == "doctorno") {
              if (row.doctorno != "") {
                  row.his_doctorno = row.doctorno;
              }
          } else if (column.property == "doctorname") {
              row.inputcode = this.$pub.CreateInputcode(row.doctorname, this, 16);
          }
      },
      currentChangeEvent({ row, $rowIndex }) {
          this.message = row.doctorname;
          this.selectrow = row;
          this.tempindex = $rowIndex;
          this.doctorno = row.doctorno;
          this.actionindex = "M";
      },
      CellClassName({ column, columnIndex, row }) {
          let loginutype = this.$tool.getSession(this.$tool.loginutype);
          if (columnIndex == "0") {
              return "col-gray";
          } else {
              if (this.$ddldata.IsAreaMode) {
                  if (column.property == "hospcode" && loginutype != "G" && loginutype != "R") {
                      return "col-gray";
                  } else {
                      return "";
                  }
              }
          }
      },
      RowClassName({ row, rowIndex }) {
          let loginutype = this.$tool.getSession(this.$tool.loginutype);
          let isDirty = this.$refs.xTable1.hasRowChange(row);
          if (this.$ddldata.IsAreaMode) {
              //区域LIS模式下:非管理员,禁止编辑Hospcode字段及Hospcode=All的整行数据
              if (loginutype != "G" && loginutype != "R") {
                  if (row.hospcode == "All") {
                      return "rm-disable";
                  }
              }
          }
          if (isDirty) {
              return "row-modify";
          } else {
              if (row.invalidflag == "1") {
                  return "rm-alert";
              }
          }
          if (this.$refs.xTable1.hasRowInsert(row)) return "row-new";
      },
      btnExport() {
          this.$refs.xTable1.exportCsv();
      },
      btnAdd() {
          let initialdata = {
              doctorno: "",
              doctorname: "",
              doctorkind: "",
              his_doctorno: "",
              shortmsgcode: "",
              inputcode: "",
              hospcode: this.$ddldata.IsAreaMode ? this.$tool.getSession(this.$tool.selectedhis) : "All",
              invalidflag: "",
              idcardno: "",
          };
          this.$refs.xTable1.insert(initialdata);
          this.FunScrollY();
          this.actionindex = "A";
      },
      FunScrollY() {
          this.$nextTick(() => {
              let alldataLen = this.$refs.xTable1.getTableData().fullData.length;
              let L = this.$refs.xTable1.getTableData().fullData[alldataLen - 1];
              this.$refs.xTable1.setCurrentRow(L);
              this.$refs.xTable1.setActiveRow(L, "doctorno");
              this.$refs.xTable1.scrollToRow(L);
          });
      },
      btnDelete() {
          let utype = this.$tool.getSession(this.$tool.loginutype);
          if (this.$ddldata.IsAreaMode && utype != "G" && utype != "R" && this.hospitalcode == "All") {
              this.$alert("您不是管理员或区域管理员，不能修改医院代码为All的数据！", "提示", {
                  confirmButtonText: "确定",
              });
              return false;
          } else {
              if (this.message == "") {
                  this.ConfirmDel();
              } else {
                  this.bDialogDelVisible = true;
              }
          }
      },
      btnFresh() {
          this.actionindex = "Q";
          this.btnQuery();
      },
      btnSave() {
          let insertresult = [];
          let xTable1data = this.$refs.xTable1.getRecordset();

          let insertobj = xTable1data.insertRecords;
          for (var i = 0; i < insertobj.length; i++) {
              var obj = insertobj[i];
              if (!!obj["doctorno"] && !!obj["doctorname"] && !!obj["his_doctorno"]) {
                  insertresult.push(obj);
              } else {
                  if (!!obj["doctorno"] || !!obj["doctorname"] || !!obj["his_doctorno"]) {
                      let msg = "医生代码(HIS)";
                      if (!obj["doctorno"]) {
                          msg = "医生代码";
                      } else if (!obj["doctorname"]) {
                          msg = "医生姓名";
                      }
                      this.$message.warning(msg + "不能为空");
                  } else if (!obj["doctorno"] && !obj["doctorname"]) {
                      this.$refs.xTable1.remove(obj);
                  }
                  break;
              }
          }
          xTable1data.insertRecords = insertresult;
          this.$refs.xTable1.validate((valid) => {
              if (valid) {
                  if (this.actionindex == "A") {
                      if (xTable1data.insertRecords.length > 0) {
                          this.selectrow = xTable1data.insertRecords[0];
                      }
                  } else if (this.actionindex == "M") {
                      if (xTable1data.updateRecords.length > 0) {
                          this.selectrow = xTable1data.updateRecords[0];
                      }
                  }
                  this.$tool
                      .request({
                          url: "MNG/MNG2/MNG212",
                          method: "post",
                          data: {
                              action: "save",
                              urec: xTable1data,
                          },
                      })
                      .then((response) => {
                          if (response.errcode == "1") {
                              this.$message({
                                  message: "操作成功！",
                                  type: "success",
                              });
                              this.btnQuery();
                              this.bQueryAll = true;
                          } else {
                              this.$message({
                                  message: "保存失败！" + response.errmsg,
                                  type: "warning",
                              });
                          }
                      })
                      .catch((e) => {
                          console.log("error", e);
                      });
              }
          });
      },
      cellClassName({ columnIndex }) {
          if (columnIndex == "0" || columnIndex == "7") {
              return "col-gray";
          } else {
              return "";
          }
      },
      ddlInitdata() {
          this.$tool
              .request({
                  url: "MNG/MNG2/MNG212",
                  method: "post",
                  data: {
                      action: "ddlbtnQuery",
                  },
              })
              .then((response) => {
                  this.HospitalOptions = response.dt1;
              });
      },
      CellActiveMethod({ row, column }) {
          let loginutype = this.$tool.getSession(this.$tool.loginutype);
          if (this.$ddldata.IsAreaMode) {
              //区域LIS模式下:非管理员,禁止编辑Hospcode字段及Hospcode=All的整行数据
              if (loginutype != "G" && loginutype != "R") {
                  if (column.property == "hospcode") {
                      return false;
                  } else {
                      if (row.hospcode == "All") {
                          return false;
                      } else {
                          return true;
                      }
                  }
              } else {
                  return true;
              }
          } else {
              return true;
          }
      },
      ConfirmDel() {
          this.bDialogDelVisible = false;
          try {
              var delrow = this.$refs.xTable1.getCurrentRow();
              if (delrow) {
                  var alld = this.$refs.xTable1.getTableData().fullData;
                  var rowindex = alld.indexOf(delrow);
                  this.$refs.xTable1.remove(delrow);
                  this.actionindex = "D";
                  let havedata = this.$refs.xTable1.getTableData().fullData;
                  if (havedata.length > 0) {
                      if (rowindex >= havedata.length) rowindex = havedata.length - 1;
                      let selectrow = havedata[rowindex];
                      this.$refs.xTable1.setCurrentRow(selectrow);
                      this.$refs.xTable1.scrollToRow(selectrow);
                      this.message = selectrow.doctorname;
                  }
              }
          } catch (e) {
              alert(e);
          }
      },
      GetUtype() {
          let loginutype = this.$tool.getSession(this.$tool.loginutype);
          if (this.$ddldata.IsAreaMode) {
              this.power4Type = true;
          } else {
              this.power4Type = false;
          }
      },
      hospitalChange(code, name) {
          this.ddlhospcode = code;
          this.btnQuery();
      },
      syncdata() {
          this.btnFresh();
          const rloading = this.openLoading("医生字典同步中，请耐心等待。");
          // this.bLoading = true;
          this.$tool
              .request({
                  url: "MNG/MNG2/MNG212",
                  method: "post",
                  timeout: 600000,
                  data: {
                      action: "syncdata",
                  },
              })
              .then((res) => {
                  if (res.err == 0) {
                      let msg = res.msg;
                      if (!msg) msg = "同步数据获取失败！";
                      // this.bLoading = false;
                      rloading.close();
                      this.$alert(msg);
                      return;
                  }
                  let msg = res.msg;
                  if (!msg) msg = "医生字典同步完成！";
                  rloading.close();
                  this.btnFresh();
                  this.$alert(msg);
              })
              .catch((e) => {
                  console.log("error", e);
                  // this.bLoading = false;
                  rloading.close();
              });
      },
      //同步添加遮罩层
      openLoading(text) {
          const rloading = this.$loading({
              // 声明一个loading对象
              lock: true, // 是否锁屏
              text: text, // 加载动画的文字
              spinner: "el-icon-loading", // 引入的loading图标
              background: "rgba(0, 0, 0, 0.3)", // 背景颜色
              target: ".sub-main", // 需要遮罩的区域
              body: true,
              customClass: "mask", // 遮罩层新增类名
          });
          return rloading;
      },
  },
};
</script>

<style scoped>
.pagetop {
  width: 100%;
  height: 35px;
  display: -webkit-box;
  -webkit-box-pack: left;
  -webkit-box-align: center;
  border: 1px solid #ccc;
  position: absolute;
  z-index: 99;
}
.pagecenter {
  position: absolute;
  top: 40px;
  width: 100%;
  left: 0;
  bottom: 0px;
  overflow: auto;
}
.middle-center {
  position: absolute;
  height: 100%;
  left: 0px;
  right: 0px;
  bottom: 0px;
  overflow: auto;
}
.table1style tr td {
  padding: 0px 2px 0px 5px;
}
.forbid {
  color: #b3b3b3 !important;
  background-color: #f5f5f5 !important;
  cursor: no-drop;
}
/deep/.middle-center tr td div .el-input__inner {
  border: 1px solid rgba(255, 254, 254, 0) !important;
  background-color: rgba(255, 254, 254, 0) !important;
}
/deep/ .mytable-style.vxe-table .vxe-body--column.col-orange {
  background-color: #ccc;
}
/deep/ tr.vxe-body--row.row-modify {
  color: blue !important;
}
/deep/ tr.vxe-body--row.row-new {
  color: #cc6600 !important;
}
/deep/ .mytable-style.vxe-table .vxe-body--column.col-gray {
  background-color: #f8f8f8;
}
</style>
